package Indexing;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import Classes.Path;

public class PreProcessedCorpusReader {
	
	// YOU CAN ADD ESSENTIAL PRIVATE METHODS OR VARIABLES ->
	private BufferedReader br;
	
    /**
     * Constructor: Initiate preprocessed collection reader.
     * @param type
     * @throws IOException
     */
	public PreProcessedCorpusReader(String dataType) throws IOException {
		// This constructor opens the preprocessed corpus file, Path.ResultHM1 + type
		// You can use your own version, or download from http://crystal.exp.sis.pitt.edu:8080/iris/resource.jsp
		// Close the file when you do not use it any more
		FileInputStream fis = new FileInputStream(Path.ResultHM1 + dataType); 
		InputStreamReader isr = new InputStreamReader(fis);
		br = new BufferedReader(isr);	
	}
	
	/**
	 * Get every doc in the given data type collection 
	 * @return
	 * @throws IOException
	 */
	public Map<String, Object> NextDocument() throws IOException {
		// Read a line for docNo and a line for content, put into the map with <docNo, content>
		Map<String, Object> docMap = new HashMap<>();
		
		String docID = "";
		String docContent = "";
	    if((docID = br.readLine()) != null) { // Get docID
	    	docContent = br.readLine(); // Get docContent
	    	docMap.put(docID, docContent.toCharArray()); // Put "docID-docContent pair" into docMap
	    	
	    	return docMap;
	    }
        
	    br.close();
		return null;
	}
}